package GUI;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import Controller.Client;
import Entity.CasualSubscription;
import Entity.Customer;
import Entity.FullSubscription;
import Entity.ParkingLot;
import Entity.Prices;

import java.awt.List;
import java.awt.Button;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;
import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Date;
import java.awt.Color;
import javax.swing.JLabel;

/**
 * this is the GUI for view subscriptions. the customer can extend his subscription for one more month or view the details.
 * @author Or Marx
 *
 */
public class SubscriptionsViewGui extends JPanel
{
	final List list;
	JFrame frame;
	JPanel lastPanel;
	public void setLastPanel (JPanel panel)
	{
		this.lastPanel=panel;
	}
	public void setFrame (JFrame frame)
	{
		this.frame=frame;
	}
	
	/**
	 * is the constructor. it has two buttons for view details or extends for 28 more days.
	 */
	public SubscriptionsViewGui() {
		setLayout(null);
		final Prices p=Client.getCurrentPrices(1);
		final JButton btnExtendFor = new JButton("Extend for 28 days");
		btnExtendFor.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Date startDate;
				Date endDate;
				Date curDate=new Date();
				Customer customers[]=Client.getCustomers();
				Customer customer = null;
				for (int i=0; i<customers.length; i++)
					if (customers[i].getcustomer_id()==Client.getLoggedIn())
						customer=customers[i];
				FullSubscription [] subs1=Client.getFullSubscriptionById(Client.getLoggedIn());
				CasualSubscription [] subs2=Client.getCasualSubscriptionById(Client.getLoggedIn());
				int i=list.getSelectedIndex();
				if (i<subs1.length)
				{
					startDate=subs1[i].getStart_date();
					endDate=subs1[i].getEnd_date();
					if (curDate.compareTo(startDate)>0 && curDate.compareTo(endDate)<0)
					{
						endDate.setTime(endDate.getTime()+1000*60*60*24*28);
						subs1[i].setEnd_date(endDate);
						Client.updateFullSubscription(subs1[i]);
						customer.AddBalance(-p.getFullMonthlySubscriber());
						JOptionPane.showMessageDialog(null,"Subscription has extended for another 28 days","extend subscription",JOptionPane.INFORMATION_MESSAGE);
					}
					else
						JOptionPane.showMessageDialog(null,"the subscription cannot be extended because its already expired","extend subscription",JOptionPane.INFORMATION_MESSAGE);

				}
				else
				{
					startDate=subs2[i-subs1.length].getStart_date();
					endDate=subs2[i-subs1.length].getEnd_date();
					if (curDate.compareTo(startDate)>0 && curDate.compareTo(endDate)<0)
					{
						endDate.setTime(endDate.getTime()+1000*60*60*24*28);
						subs2[i-subs1.length].setEnd_date(endDate);
						Client.updateCasualSubscription(subs2[i-subs1.length]);
						customer.AddBalance(-p.getMonthlyRegularSubscriber());
						JOptionPane.showMessageDialog(null,"Subscription has extended for another 28 days","extend subscription",JOptionPane.INFORMATION_MESSAGE);
					}
					else
						JOptionPane.showMessageDialog(null,"the subscription cannot be extended because its already expired","extend subscription",JOptionPane.INFORMATION_MESSAGE);
				}
			}
		});
		btnExtendFor.setEnabled(false);
		btnExtendFor.setBounds(252, 231, 166, 23);
		add(btnExtendFor);
		final JButton btnDetails = new JButton("details");
		btnDetails.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Date startDate;
				Date endDate;
				Date curDate=new Date();
				FullSubscription [] subs1=Client.getFullSubscriptionById(Client.getLoggedIn());
				CasualSubscription [] subs2=Client.getCasualSubscriptionById(Client.getLoggedIn());
				int i=list.getSelectedIndex();
				if (i<subs1.length)
				{
					startDate=subs1[i].getStart_date();
					endDate=subs1[i].getEnd_date();
				}
				else
				{
					startDate=subs2[i-subs1.length].getStart_date();
					endDate=subs2[i-subs1.length].getEnd_date();
				}
				JOptionPane.showMessageDialog(null,"Activated at: "+startDate.toString() + "\nEnds at: "+ endDate.toString(),"Full Subscription",JOptionPane.INFORMATION_MESSAGE);
			}
		});
		btnDetails.setEnabled(false);
		btnDetails.setBounds(175, 266, 89, 23);
		add(btnDetails);
		
		
		list = new List();
		list.setForeground(Color.BLACK);
		list.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				if (list.getSelectedIndex()!=-1)
				{
					btnExtendFor.setEnabled(true);
					btnDetails.setEnabled(true);
				}
				else
				{
					btnDetails.setEnabled(false);
					btnExtendFor.setEnabled(false);
				}
					
					
			}
		});
		list.setFont(new Font("Dialog", Font.PLAIN, 16));
		list.setMultipleSelections(false);
		list.setBounds(10, 81, 607, 144);
		FullSubscription [] subs1=Client.getFullSubscriptionById(Client.getLoggedIn());
		for(int i=0;i<subs1.length; i++)
			list.add("subscription number: "+subs1[i].getSubscription_number()+"      carNumber: "+ subs1[i].getCar_number()+"    Subscription type: Full");
		CasualSubscription [] subs2=Client.getCasualSubscriptionById(Client.getLoggedIn());
		for(int i=0;i<subs2.length; i++)
			list.add("subscription number: "+subs2[i].getSubscription_number()+"      carNumber: "+ subs2[i].getCar_number()+ "     Subscription type: Casual       Parking lot: "+Client.getParkingLotNameByNumber(subs2[i].getParking_lot_number())+"       Leaving hour: "+subs2[i].getleaving_time());
		
		add(list);
		
		Button button = new Button("Cancel");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				frame.setContentPane((JPanel)lastPanel);
			}
		});
		button.setBounds(417, 270, 69, 19);
		add(button);
		
		JLabel lblPriceForExtending = new JLabel("Price for extending Full Subscription: "+ p.getFullMonthlySubscriber()+"$");
		lblPriceForExtending.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblPriceForExtending.setBounds(117, 11, 480, 23);
		add(lblPriceForExtending);
		
		JLabel lblPriceForExtending_1 = new JLabel("Price for extending Casual Subscription: " + p.getMonthlyRegularSubscriber()+"$");
		lblPriceForExtending_1.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblPriceForExtending_1.setBounds(97, 45, 480, 23);
		add(lblPriceForExtending_1);
	}
}
